package com.threeti.mecool.core.domain.model.project;

import com.threeti.mecool.core.domain.model.Hint;
import com.threeti.mecool.core.domain.model.HintKey;
import com.threeti.mecool.core.exception.PreSaveValidateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "DM_PROJECT_SU")
@Configurable
public class ProjectSellingUpload {
    private Long id;
    private Integer version;
    private Project project;
    private ProjectProductCollectionType uploadType;
    private int uploadSummary;
    private int uploadPrice;
    private int uploadAmount;
    private String uploadAmountUnit;
    private Set<String> codes = new HashSet<String>();
    private Date updatedAt;
    @Autowired
    private Hint hint;

    @PrePersist
    @PreUpdate
    void changeValues() {
        if (uploadType != ProjectProductCollectionType.SKU) setUploadPrice(0);
        if (1 != uploadAmount) setUploadAmountUnit(null);
        if (uploadType == ProjectProductCollectionType.SKU) {
            if (uploadPrice == 0 && uploadAmount == 0) {
                throw new PreSaveValidateException(hint.get(HintKey.SU_AMOUNT_OR_PRICE_REQUIRED));
            }
        }
        updatedAt = new Date();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    public Long getId() {
        return id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    @OneToOne
    public Project getProject() {
        return project;
    }

    @Enumerated(EnumType.STRING)
    public ProjectProductCollectionType getUploadType() {
        return uploadType;
    }

    @ElementCollection
    @CollectionTable(name = "DM_PROJECT_SU_CODE")
    public Set<String> getCodes() {
        return codes;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Date getUpdatedAt() {
        return updatedAt;
    }

    public int getUploadSummary() {
        return uploadSummary;
    }

    public int getUploadPrice() {
        return uploadPrice;
    }

    public int getUploadAmount() {
        return uploadAmount;
    }

    public String getUploadAmountUnit() {
        return uploadAmountUnit;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public void setUploadType(ProjectProductCollectionType uploadType) {
        this.uploadType = uploadType;
    }

    public void setCodes(Set<String> codes) {
        this.codes = codes;
    }

    public void setUploadSummary(int uploadSummary) {
        this.uploadSummary = uploadSummary;
    }

    public void setUploadPrice(int uploadPrice) {
        this.uploadPrice = uploadPrice;
    }

    public void setUploadAmount(int uploadAmount) {
        this.uploadAmount = uploadAmount;
    }

    public void setUploadAmountUnit(String uploadAmountUnit) {
        this.uploadAmountUnit = uploadAmountUnit;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }
}
